package com.ai.web.sendreq;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;

public class RequestApp {

	private static final String charSet = "UTF-8";
	
	public static String request(String url, String xmlDom)
			throws Exception {

		HttpClient httpClient = new HttpClient();
		String result = null;
		HttpConnectionManagerParams managerParams = httpClient.getHttpConnectionManager().getParams();
		// 设置连接超时时间(单位毫秒)
		managerParams.setConnectionTimeout(30000);
		// 设置读数据超时时间(单位毫秒)
		managerParams.setSoTimeout(120000); 
		PostMethod httpPost = new PostMethod(url);
		if (xmlDom == null) {
			try {
				// httpPost.setRequestHeader("Content-type",
				// "application/x-www-form-urlencoded");
				// httpPost.addRequestHeader("Cache-Control","no-cache");
				// httpPost.addRequestHeader("Connection","Keep-Alive");
				// httpPost.addRequestHeader("User-Agent",
				// "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
				// httpPost.addRequestHeader("Accept-Encoding","gzip, deflate");
				// httpPost.addRequestHeader("Accept-Language","zh-cn");
				httpClient.executeMethod(httpPost);
				InputStream inputStream = httpPost.getResponseBodyAsStream();  
		        BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, charSet));  
		        StringBuffer stringBuffer = new StringBuffer();  
		        String str= "";  
		        
		        while((str = br.readLine()) != null){   
		            stringBuffer.append(str);
		        } 		        
				result = stringBuffer.toString();
			} catch (Exception e) {
				e.printStackTrace();
				throw new Exception(e.getMessage());
			} finally {
				httpPost.releaseConnection();
			}
		} else {
			try{
				RequestEntity entity = new StringRequestEntity(xmlDom.toString(),
						"text/xml", "UTF-8");
				httpPost.setRequestEntity(entity);
				httpPost.setRequestHeader("Content-type", "text/xml;charset=UTF-8");
				// httpPost.addRequestHeader("Cache-Control","no-cache");
				// httpPost.addRequestHeader("Connection","Keep-Alive");
				// httpPost.addRequestHeader("User-Agent",
				// "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
				// httpPost.addRequestHeader("Accept-Encoding","gzip, deflate");
				// httpPost.addRequestHeader("Accept-Language","zh-cn");
				httpClient.executeMethod(httpPost);
				InputStream inputStream = httpPost.getResponseBodyAsStream();  
		        BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, charSet));  
		        StringBuffer stringBuffer = new StringBuffer();  
		        String str= "";  
		        
		        while((str = br.readLine()) != null){   
		            stringBuffer.append(str);
		        }   
		        
				result = stringBuffer.toString();
			} catch (Exception e) {
				e.printStackTrace();
				throw new Exception(e.getMessage());
			} finally {
				httpPost.releaseConnection();
			}
		}
		return result;

	}
}
